package com.sanjiahantang.youjiaclient.module.money.views.impl;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.sanjiahantang.plugin.pickerview.OptionsPickerView;
import com.sanjiahantang.plugin.pickerview.TimePickerView;
import com.sanjiahantang.youjiaclient.module.money.presenters.MoneyPresenter;
import com.sanjiahantang.youjiaclient.module.money.views.MoneyView;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.module.common.beans.ConditionBean;
import com.sanjiahantang.youjiaclient.module.common.utils.CommonModelUtils;
import com.sanjiahantang.youjiaclient.module.common.views.impl.SelectorActivity;
import com.sanjiahantang.youjiaclient.utils.KeyBoardUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 添加银行卡
 * Created by admin on 2017/10/28.
 */

public class MeAddBankActivity extends BaseMvpActivity<MoneyView, MoneyPresenter> implements MoneyView, View.OnClickListener, View.OnTouchListener {
    private View fl_Left, selectBank;
    private TextView tvBankName, tvBankNo, tvHolder, tvIdCard, tvPhone, tvType, tvCVV, tvDate, tvOpenBank, tvBankAddress;
    private View vBankName, vType, vCVV, vDate, vBankAddress, btn_beSure;


    private String strBankName, strBankNo, strBankCode, strHolder, strPhone, strType, strDate, strOpenBank, strBankAddress, strProvince, strCity;
    private final int INTENT_BANK = 1, INTENT_TYPE = 2;
    private ArrayList<ConditionBean.SelectorData> bankList, typeList;
    private ConditionBean.SelectorData bankData, typeData;
    private OptionsPickerView pvOptions;
    private TimePickerView pvTime;

    private ArrayList<String> listProvince;
    private ArrayList<ArrayList<String>> listCity;

    @Override
    public MoneyPresenter initPresenter() {
        return new MoneyPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_add_bank);
        fl_Left = findViewById(R.id.fl_Left);
        fl_Left.setOnClickListener(this);
        TextView title = (TextView) findViewById(R.id.title);
        title.setText("添加银行卡");
        initTypeList();
        initArea();
        initDateDialog();

        tvBankName = (TextView) findViewById(R.id.tvBankName);
        tvBankNo = (TextView) findViewById(R.id.tvBankNo);
        tvHolder = (TextView) findViewById(R.id.tvHolder);
        tvIdCard = (TextView) findViewById(R.id.tvIdCard);
        tvPhone = (TextView) findViewById(R.id.tvPhone);
        tvType = (TextView) findViewById(R.id.tvType);
        tvCVV = (TextView) findViewById(R.id.tvCVV);
        tvDate = (TextView) findViewById(R.id.tvDate);
        tvOpenBank = (TextView) findViewById(R.id.tvOpenBank);
        tvBankAddress = (TextView) findViewById(R.id.tvBankAddress);

        vBankName = findViewById(R.id.vBankName);
        vType = findViewById(R.id.vType);
        vCVV = findViewById(R.id.vCVV);
        vDate = findViewById(R.id.vDate);
        vBankAddress = findViewById(R.id.vBankAddress);
        btn_beSure = findViewById(R.id.btn_beSure);

        vBankName.setOnClickListener(this);
        vType.setOnClickListener(this);
        tvDate.setOnClickListener(this);
        vDate.setOnClickListener(this);
        vBankAddress.setOnClickListener(this);
        btn_beSure.setOnClickListener(this);
    }

    private void initTypeList() {

        typeList = new ArrayList<>();
        ConditionBean.SelectorData type1 = new ConditionBean.SelectorData();
        type1.setDicName("储蓄卡");
        type1.setDicValue("01");
        typeData = type1;

        ConditionBean.SelectorData type2 = new ConditionBean.SelectorData();
        type2.setDicName("信用卡");
        type2.setDicValue("02");

        typeList.add(type1);
        typeList.add(type2);
    }

    private void initArea() {
        CommonModelUtils.getAllArea($this, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                Object[] listArray = (Object[]) content;
                listProvince = (ArrayList<String>) listArray[0];
                listCity = (ArrayList<ArrayList<String>>) listArray[1];
                pvOptions = new OptionsPickerView.Builder($this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        strProvince = listProvince.get(options1);
                        strCity = listCity.get(options1).get(options2);
                        String tx = strProvince + " " + strCity;
                        tvBankAddress.setText(tx);

                    }
                })
                        .setTitleText("")
                        .setDividerColor(Color.BLACK)
                        .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                        .setContentTextSize(20)
                        .build();

                pvOptions.setPicker(listProvince, listCity);//三级选择器
            }
        });
    }

    private void initDateDialog() {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(selectedDate.get(Calendar.YEAR), selectedDate.get(Calendar.MONTH), selectedDate.get(Calendar.DAY_OF_MONTH));
        Calendar endDate = Calendar.getInstance();
        endDate.set(selectedDate.get(Calendar.YEAR) + 100, selectedDate.get(Calendar.MONTH), selectedDate.get(Calendar.DAY_OF_MONTH));
        //时间选择器
        pvTime = new TimePickerView.Builder($this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
                /*btn_Time.setText(getTime(date));*/
                tvDate.setText(getTime(date));
                strDate = getPostTime(date);
            }
        })
                //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, false, false, false, false})
                .setLabel("", "", "", "", "", "")
                .isCenterLabel(false)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(21)
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setBackgroundId(0x00FFFFFF) //设置外部遮罩颜色
                .setDecorView(null)
                .build();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy 年 MM 月");
        return format.format(date);
    }

    private String getPostTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("MMyy");
        return format.format(date);
    }

    @Override
    public void initData() {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void onClick(View view) {
        KeyBoardUtils.closeKeyboardWithoutFinish($this);
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.vBankName:
                if (bankList == null) {
                    CommonModelUtils.findDicInfoByParentIdForCommon($this, CommonModelUtils.TYPE_BANK, new OnLoadListener() {
                        @Override
                        public void onLoad(Object content) {
                            if (content != null) {
                                if (content instanceof List) {
                                    bankList = (ArrayList<ConditionBean.SelectorData>) content;
                                    SelectorActivity.startActivity($this, bankList, "选择银行", INTENT_BANK);
                                }
                            }
                        }
                    });
                } else {
                    SelectorActivity.startActivity($this, bankList, "选择银行", bankData == null ? null : bankData.getDicValue(), INTENT_BANK);
                }

                break;
            case R.id.vType:
                SelectorActivity.startActivity($this, typeList, "选择类型", typeData == null ? null : typeData.getDicValue(), INTENT_TYPE);
                break;
            case R.id.btn_beSure:
                presenter.addBankCardInfo($this,
                        bankData,
                        tvBankNo.getText().toString(),
                        tvHolder.getText().toString(),
                        tvIdCard.getText().toString(),
                        tvPhone.getText().toString(),
                        typeData,
                        tvCVV.getText().toString(),
                        strDate,
                        tvOpenBank.getText().toString(),
                        strProvince,
                        strCity,
                        new OnLoadListener() {
                            @Override
                            public void onLoad(Object content) {
                                if (content != null) {
//                            finish();
                                    MeAddBankVerifyActivity.startActivity($this, tvBankNo.getText().toString());
                                }
                            }
                        });
                break;
            case R.id.vDate:
            case R.id.tvDate:
                pvTime.show();
                break;
            case R.id.vBankAddress:
                pvOptions.show();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case INTENT_BANK:
                    bankData = (ConditionBean.SelectorData) data.getSerializableExtra(KEY_PARAM);
                    if (bankData != null) {
                        tvBankName.setText(bankData.getDicName());
                    }
                    break;
                case INTENT_TYPE:
                    typeData = (ConditionBean.SelectorData) data.getSerializableExtra(KEY_PARAM);
                    if (typeData != null) {
                        tvType.setText(typeData.getDicName());
                        if (TextUtils.equals("01", typeData.getDicValue())) {
                            vCVV.setVisibility(View.GONE);
                            vDate.setVisibility(View.GONE);
                        } else {
                            vCVV.setVisibility(View.VISIBLE);
                            vDate.setVisibility(View.VISIBLE);
                        }
                    }
                    break;
            }
        }
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {

        return false;
    }
}
